<template>
  <!--录入入库信息-->
  <el-dialog :title="readonly?'查看出库详情':'新增出库信息'" :visible.sync="goodOutDialogVisible" width="50%" center
             @close="closeDialog" @open="openDialog" :append-to-body="appendToBody"
  >
    <el-form ref="goodOutForm" :model="goodOut" class="dialogForm" :rules="goodOutFromRules" v-loading="dialogLoading">
      <table class="mailTable">
        <tr class="firstTr">
          <td class="column tip_td firstTd">
            <span class="tip">*</span>物品
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="goods_type_id">
              <el-select v-model="goodOut.goods_type_id" clearable filterable allow-create placeholder="请选择物品"
                         style="width: 100%" @change="getStorageNumber"
              >
                <el-option v-for="item in goods_type_arr" :key="item.id" :value="item.id" :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <span v-else>{{ goodOut.good_name }}</span>
          </td>
        </tr>
        <tr>
          <td class="column tip_td firstTd">
            <span class="tip">*</span>数量
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="number">
              <el-input v-model.trim="goodOut.number" v-loading="inputLoading" type="number" step="1"
                        placeholder="请输入数量"
              >
                <template slot="append">
                  库存{{ num }}个
                </template>
              </el-input>
            </el-form-item>
            <span v-else>
              {{ goodOut.number }}
            </span>
          </td>
        </tr>
        <tr>
          <td class="column tip_td firstTd">
            <span class="tip">*</span>用途
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="reason">
              <el-input v-model.trim="goodOut.reason" type="textarea" placeholder="请输入用途" autosize
              ></el-input>
            </el-form-item>
            <span v-else>{{ goodOut.reason }}</span>
          </td>
        </tr>
        <tr>
          <td class="column tip_td firstTd">
            <span class="tip">*</span>领用人
          </td>
          <td class="column">
            <el-input v-model.trim="goodOut.user_name" v-loading="inputLoading" type="text"
                      placeholder="请输入领用人">
          </td>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="goodOutDialogVisible = false">{{ readonly ? '关闭' : '取消' }}</el-button>
      <el-button v-if="!readonly" size="mini" type="primary" @click="saveInfo()">保&nbsp;存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { detail, getStorageNumber, save } from '@/api/goods/goods_out'
import { storage_list } from '@/api/goods/goods_storage_manage'
import {multiply} from "@/utils/amount";

export default {
  name: 'GoodsOutDialog',
  props: {
    readonly: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number,
      default: -1
    },
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogLoading: false,
      goodOutDialogVisible: false,
      goodOut: {
        id: null,
        goods_type_id: null,
        number: null,
        reason: null,
        user_name: ''
      },
      num: 0,
      goodOutFromRules: {
        goods_type_id: [
          { required: true, message: '请选择物品', trigger: 'change' }
        ],
        number: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '请输入事由', trigger: 'blur' }
        ]
      },
      inputLoading: false,
      goods_type_arr: []
    }
  },
  watch: {
    'goodOut.goods_type_id': function(val) {
      if (val && !isNaN(val)) {
        this.getStorageNumber(val)
      }
    }
  },
  computed: {
    user_name() {
      return !this.goodOut.user_name ? this.$store.state.user.name : this.goodOut.user_name
    }
  },
  methods: {
    closeDialog() {
      this.goodOut = {
        group_id: null,
        goods_type_name: null,
        number: null,
        price: null,
        total_price: null,
        product_date: null,
        due_date: null,
        user_id: null
      }
      this.num = 0
      this.$nextTick(() => {
        this.$refs.goodOutForm.clearValidate()
      })
    },
    saveInfo() {
      this.$refs.goodOutForm.validate(flag => {
        if (flag) {
          const loading = this.$loading({
            lock: true,
            text: '保存中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          save(this.goodOut).then(res => {
            loading.close()
            this.$message.success('操作成功')
            this.goodOutDialogVisible = false
            this.$emit('reload')
          }).catch(() => {
            loading.close()
          })
        }
      })
    },
    show() {
      this.goodOutDialogVisible = true
    },
    getStorageNumber(id) {
      if (id) {
        this.inputLoading = true
        getStorageNumber({ id }).then(res => {
          this.inputLoading = false
          this.num = res.data
        }).catch(() => {
          this.inputLoading = false
        })
      } else {
        this.num = 0
      }
    },
    openDialog() {
      if (this.readonly && this.id !== -1) {
        this.dialogLoading = true
        detail(this.id).then(res => {
          this.goodOut = res.data
          this.dialogLoading = false
        }).catch(() => {

        })
      }
      this.initGoodsType()
    },
    initGoodsType() {
      storage_list().then(res => {
        this.goods_type_arr = res.data
      })
    }
  }
}
</script>

<style scoped>
.mailTable {
  width: 90%;
  font-size: 14px;
  margin: 0 auto 10px auto;
  border-collapse: collapse;
}

.mailTable tr td {
  width: 200px;
  height: 60px;
  line-height: 35px;
  box-sizing: border-box;
  padding: 0 10px;
  border-bottom: 1px solid #E6EAEE;
  border-right: 1px solid #E6EAEE;
}

.firstTr td {
  border-top: 1px solid #E6EAEE;
}

.firstTd {
  border-left: 1px solid #E6EAEE;
}

.mailTable tr td.column {
  text-align: center;
}

.tip {
  color: red;
}

/deep/ .dialogForm .el-form-item {
  margin-bottom: 0;
}

.mailTable .tip_td {
  width: 30%;
}
</style>
